<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="./assets/css/bootstrap.min.css">
    <script src="./assets/js/jquery.js"></script>
    <script src="./assets/js/template-web.js"></script>
</head>
<body>
    <div class="container">
        <h2 style="color:#666">编辑学生信息</h2>
        <form id="form">
            
        </form>
    </div>

    <script type="text/tpl" id="edit-form">
        <input type="hidden" name="sno" value="{{data.sno}}">
        <div class="form-group">
            <label for="name">姓名</label>
            <input type="text" name="sname" class="form-control" value="{{data.sname}}">
        </div>
        <div class="form-group">
            <label for="name">昵称</label>
            <input type="text" name="snickname" class="form-control" value="{{data.snickname}}">
        </div>
        <div class="form-group">
            <label for="age">年龄</label>
            <input type="text" name="sage" class="form-control" value="{{data.sage}}">
        </div>
        <div class="form-group">
            <label for="name">性别</label>&emsp;
            <label class="radio-inline">
                <input type="radio" name="sgender" value="男" {{data.sgender === '男' ? 'checked' : ''}}> 男
            </label>
            <label class="radio-inline">
                <input type="radio" name="sgender" value="女" {{data.sgender === '女' ? 'checked' : ''}}> 女
            </label>
        </div>
        <div class="form-group">
            <label for="age">学院</label>
            <input type="text" name="sdept" class="form-control" value="{{data.sdept}}">
        </div>
        <button type="button" class="btn btn-primary">修改</button>
    </script>

    <script>
        var sno = location.search.slice(5)

        $.get(`/school/api/v1.0/student/${sno}`, function (response) {
            console.log(response);
            var str = template("edit-form", {data: response.data})
            $('#form').html(str);
        })

        $('form').on('click', '.btn-primary', function () {
            console.log(123);
            var sno = $('[name=sno]').val()
            var sname = $('[name=sname]').val()
            var snickname = $('[name=snickname]').val()
            var sage = $('[name=sage]').val()
            var sgender = $('[name=sgender]').val()
            var sdept = $('[name=sdept]').val()

            $.ajax({
                url: "/school/api/v1.0/student/" + sno,
                method: "put",
                data : `sname=${sname}&snickname=${snickname}&sage=${sage}&sgender=${sgender}&sdept=${sdept}`,
                success (response) {
                    console.log(response.code);
                    if (response.code == 200) {
                        alert(response.msg)
                        // $('#add-form').reset()
                        // document.querySelector('#add-form').reset()
                    } else {
                        alert("修改失败");
                    }
                }
            })
        })
            
    </script>
</body>
</html>
